using System;
using System.Linq;
using CommandSystem;
using Exiled.API.Features;
using RgCore.GameData;
using RgCore.Menu;
using RgCore.PlayerHud;

[CommandHandler(typeof(ClientCommandHandler))]
public class MenuCommand : ICommand
{
    public string[] Aliases => Array.Empty<string>();

    public string Command => "menu";

    public string Description => "控制菜单";

    public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
    {
        response = string.Empty;
        Player player = Player.Get(sender);
        MenuInterface mi = MenuInterface.MenuInterfaces.FirstOrDefault(x => x.Player == player);
        string operation = arguments.At(0);
        if (mi.LastOperation.AddSeconds(0.3) > DateTime.Now)
        {
            if (!player.HasMsg("too_fast"))
                player.SendFormattedMsg("too_fast", "操作过快", "短时间内操作过快，喝杯茶休息一下吧", 2, MsgType.Notification,
                    ScreenLocation.CenterTop, true);
        }
        else if (operation == "show" && mi.CurrentPage == null)
        {
            var data = GameplayData.GameDatas.FirstOrDefault(x => x.Player == player);
            if (!data.KeyBinded)
            {
                data.KeyBinded = true;
                player.SendRawMsg("success", "<color=#00FF00>[按键已绑定]</color> 检测到按键命令，感谢您的支持，游戏愉快！", 5,
                    ScreenLocation.Top, true);
            }

            mi.Activate();
        }
        else
        {
            switch (operation)
            {
                case "back":
                    mi.Back();
                    break;
                case "select":
                    mi.Select(int.Parse(arguments.At(1)));
                    break;
                case "down":
                    mi.DownPointer();
                    break;
                case "up":
                    mi.UpPointer();
                    break;
                case "nextpage":
                    mi.NextPage();
                    break;
                case "prevpage":
                    mi.PrevPage();
                    break;
            }
        }

        return true;
    }
}